package com.juguo.charginganimation.View.fragment;

import androidx.databinding.DataBindingUtil;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;


import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.gyf.barlibrary.ImmersionBar;
import com.juguo.charginganimation.Adapter.ViewpagerFragmentAdapter;
import com.juguo.charginganimation.R;
import com.juguo.charginganimation.View.HomeFragment.FeaturedFragment;
import com.juguo.charginganimation.utils.ToastUtil;
import com.juguo.charginganimation.utils.UITools;
import com.juguo.charginganimation.View.HomeFragment.McoolFragment;
import com.juguo.charginganimation.View.HomeFragment.MenuRockFragment;
import com.juguo.charginganimation.View.HomeFragment.MenuanimeFragment;
import com.juguo.charginganimation.View.HomeFragment.MenuchristmasFragment;
import com.juguo.charginganimation.View.HomeFragment.MenudancingFragment;
import com.juguo.charginganimation.Viewmodel.AnimationViewModel;
import com.juguo.charginganimation.databinding.AnimationFragmentBinding;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;

import static android.content.ContentValues.TAG;


public class AnimationFragment extends Fragment {
    private Fragment mSecondFragment = null;
    private DrawerLayout drawerLayout; //抽屉
    private AnimationViewModel mViewModel;
    private AnimationFragmentBinding binding;
    private RelativeLayout relativeLayout;
    private ViewPager2 viewPager2;
    private TabLayout tableLayout;

    public static AnimationFragment newInstance() {
        return new AnimationFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.animation_fragment, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onclick(getView());
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(AnimationViewModel.class);
        //修复标题栏与状态栏重叠
        UITools.fitTitleBar(getActivity(), getView());
        //解决白色状态栏
        ImmersionBar.with(getActivity())
                .statusBarDarkFont(true)
                .navigationBarDarkIcon(true)
                .init();
        init();
    }

    private void init() {
        relativeLayout = getActivity().findViewById(R.id.rly_huanfu);
        tableLayout = getActivity().findViewById(R.id.tabLayout);
        viewPager2 = getActivity().findViewById(R.id.vp2);
        viewPager2.setAdapter(new ViewpagerFragmentAdapter(getActivity()));
        TabLayoutMediator tab = new TabLayoutMediator(tableLayout, viewPager2, (tab1, position) -> {
            switch (position) {
                case 0:
                    tab1.setText("精选");
                    break;
                case 1:
                    tab1.setText("热门");
                    break;
            }
        });
        tab.attach();
    }

    public void onclick(View view) {
        drawerLayout = getActivity().findViewById(R.id.drawerLayout);
        getActivity().findViewById(R.id.imageButton_more).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(Gravity.RIGHT);
            }
        });
        /**
         * 抽屉item点鸡
         */
        binding.menucool.setOnClickListener(v -> {
            mSecondFragment = new FeaturedFragment();
            if (mSecondFragment != null) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                FragmentTransaction beginTransaction = fm.beginTransaction();
                beginTransaction.replace(R.id.rly_huanfu, new McoolFragment());
                beginTransaction.addToBackStack(null);
                beginTransaction.commit();
            } else {
                ToastUtil.showToast(getContext(), "有了");
            }
            //这里有空优化下..................................................？？？？？？？？？？？？？？？》》》》》》》》
            relativeLayout.setVisibility(View.VISIBLE);
            if (drawerLayout != null) {
                if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                    drawerLayout.closeDrawers();
                    viewPager2.setVisibility(View.GONE);
                }
            }
        });

        binding.menudancing.setOnClickListener(v -> {
            FragmentManager fm = getActivity().getSupportFragmentManager();
            FragmentTransaction beginTransaction = fm.beginTransaction();
            beginTransaction.replace(R.id.rly_huanfu, new MenudancingFragment());
            beginTransaction.commit();
            relativeLayout.setVisibility(View.VISIBLE);
            //这里有空优化下..................................................？？？？？？？？？？？？？？？》》》》》》》》
            if (relativeLayout != null) {
                viewPager2.setVisibility(View.GONE);
            }
            if (drawerLayout != null) {
                if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                    drawerLayout.closeDrawers();
                }
            }
        });

        binding.menuanime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                FragmentTransaction beginTransaction = fm.beginTransaction();
                beginTransaction.replace(R.id.rly_huanfu, new MenuanimeFragment());
                beginTransaction.commit();
                relativeLayout.setVisibility(View.VISIBLE);
                //这里有空优化下..................................................？？？？？？？？？？？？？？？》》》》》》》》
                if (relativeLayout != null) {
                    viewPager2.setVisibility(View.GONE);
                }
                if (drawerLayout != null) {
                    if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                        drawerLayout.closeDrawers();
                    }
                }
            }
        });

        binding.menuchristmas.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                FragmentTransaction beginTransaction = fm.beginTransaction();
                beginTransaction.replace(R.id.rly_huanfu, new MenuchristmasFragment());
                beginTransaction.commit();
                relativeLayout.setVisibility(View.VISIBLE);
                //这里有空优化下..................................................？？？？？？？？？？？？？？？》》》》》》》》
                viewPager2.setVisibility(View.GONE);
                if (drawerLayout != null) {
                    if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                        drawerLayout.closeDrawers();
                    }
                }
            }
        });

        binding.menurock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getActivity().getSupportFragmentManager();
                FragmentTransaction beginTransaction = fm.beginTransaction();
                beginTransaction.replace(R.id.rly_huanfu, new MenuRockFragment());
                beginTransaction.commit();
                relativeLayout.setVisibility(View.VISIBLE);
                //这里有空优化下..................................................？？？？？？？？？？？？？？？》》》》》》》》
                viewPager2.setVisibility(View.GONE);
                if (drawerLayout != null) {
                    if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
                        drawerLayout.closeDrawers();
                    }
                }
            }
        });
    }
}